#!/bin/bash

# cantas - Startup script for Cantas web service
#
# This is script should run after all dependent services are launched.
#

# chkconfig: 345 80 20
# description: script to control Cantas web service.
#
# processname: cantas
# pidfile: /var/run/cantas.pid
#

# Source function library.
. /etc/rc.d/init.d/functions

export NODE_ENV=${NODE_ENV:="production"}

NAME=cantas
INSTANCE_DIR=/usr/lib/node_modules/$NAME
COMMAND=node
SOURCE_NAME=app.js
CANTAS_LOG_PATH=/var/log/$NAME

user=cantas
pidfile=/var/run/$NAME.pid
forever_dir=/var/run/forever
logfile=$CANTAS_LOG_PATH/$NAME.log
errlogfile=$CANTAS_LOG_PATH/$NAME-err.log

node=node
forever=forever
awk=awk
sed=sed
sudo=sudo

start()
{
    echo "Starting $NAME node instance: "

    if [ "$id" = "" ]; then
        touch $logfile
        chown $user $logfile
        touch $errlogfile
        chown $user $errlogfile

        touch $pidfile
        chown $user $pidfile

        [ -d $forever_dir ] || (mkdir -p $forever_dir )

        # Launch the application
        daemon \
            $forever start --minUptime 1000 --spinSleepTime 1000 -p $forever_dir --pidFile $pidfile -l $logfile -e $errlogfile -a --sourceDir $INSTANCE_DIR -c $COMMAND $SOURCE_NAME
        RETVAL=$?
    else
        echo "Instance already running"
        RETVAL=0
    fi
}

restart()
{
    echo -n "Restarting $NAME node instance : "
    if [ "$id" != "" ]; then
	$forever restart -p $forever_dir $id
        RETVAL=$?
    else
        start
    fi
}

stop()
{
    echo -n "Shutting down $NAME node instance : "
    if [ "$id" != "" ]; then
        $forever stop -p $forever_dir $id
    else
        echo "Instance is not running";
    fi
    RETVAL=$?
}

getForeverId() {
  touch $pidfile
  chown $user $pidfile

  local pid=$(cat $pidfile)
  $forever list |$sed -e '1,2d' | $awk '{print $3}';
}
id=$(getForeverId)

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        $forever list
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL
